package org.study.swing.study01.component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 作用描述
 * 参考 https://blog.csdn.net/hitmengfanchao/article/details/137067141
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年11月15日
 */
public class DialogDemo3 {
    public static void main(String[] args) {
        testDialog1();
    }

    public static void testDialog1() {
        JFrame frame = new JFrame("示例：JDialog");

        JTextField textField = new JTextField(12);
        JTextArea textArea = new JTextArea();
        JButton showButton = new JButton("打开对话框");
        JButton sendButton = new JButton("发送信息");
        MyDialogDemo1 myDialogDemo1 = new MyDialogDemo1(frame, "打开的对话框", false, textArea);
        // 显示对话框按钮注册事件监听器
        showButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 设置对话框为可视
                myDialogDemo1.setVisible(true);
            }
        });
        // 发送信息按钮注册事件监听器
        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 向对话框中文本区组件添加信息
                myDialogDemo1.textArea.append(textField.getText() + "\n");
            }
        });
        JPanel panel1 = new JPanel();
        panel1.add(new JLabel("传递给对话框的信息："));
        panel1.add(textField);
        JPanel panel2 = new JPanel();
        panel2.add(showButton);
        panel2.add(sendButton);
        frame.add(panel1, BorderLayout.NORTH);
        frame.add(textArea, BorderLayout.CENTER);
        frame.add(panel2, BorderLayout.SOUTH);

        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    static class MyDialogDemo1 extends JDialog {
        JTextField textField = new JTextField(12);
        JTextArea textArea = new JTextArea();
        JButton sendButton = new JButton("发送信息");
        JButton closeButton = new JButton("关闭对话框");

        public MyDialogDemo1(Frame owner, String title, boolean modal, JTextArea text) {
            super(owner, title, modal);// 调用父类的构造方法
            super.setSize(400, 300);
            super.setLocationRelativeTo(null);
            JPanel panel1 = new JPanel();
            panel1.add(new JLabel("传递给窗口的信息："));
            panel1.add(textField);
            JPanel panel2 = new JPanel();
            panel2.add(sendButton);
            panel2.add(closeButton);
            super.add(panel1, BorderLayout.NORTH);
            super.add(textArea, BorderLayout.CENTER);
            super.add(panel2, BorderLayout.SOUTH);
            // 发送信息按钮注册事件监听器
            sendButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // 向传递给对话框的窗口文本区组件添加信息
                    text.append(textField.getText() + "\n");
                }
            });
            // 关闭对话框按钮注册事件监听器
            closeButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // 设置对话框为不可视
                    setVisible(false);
                }
            });
        }
    }
}
